package com.kcs.fxmargin.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.kcs.common.utils.Constant;
import com.kcs.fxmargin.model.CConfigSys;
import com.kcs.fxmargin.model.MCurrency;

public class CommonDao {
	private Logger log = Logger.getLogger(Constant.LOG_FX);
	
	public int getRowVersion(Connection conn, PreparedStatement pstm, ResultSet rs, String tableName, String id) throws Exception{
		StringBuffer sql 		= new StringBuffer();
		int rowVersion	= 0;
		
		sql.append(" select row_version ");
		sql.append(" from " + tableName + " ");
		sql.append(" where id = ?");
		
		log.debug("SQL -> " + sql.toString());
		
		pstm = conn.prepareStatement(sql.toString());
		pstm.setString(1, id);
		
		rs = pstm.executeQuery();
		
		while(rs.next()){
			rowVersion = rs.getInt("row_version");
		}
		
		log.debug("RowVersion = " + rowVersion);
		
		return rowVersion;
	}
	
	public List<MCurrency> getCurrencyList(Connection conn, PreparedStatement pstm, ResultSet rs) throws Exception {
		StringBuffer sql 				= new StringBuffer();
		MCurrency currency				= null;
		List<MCurrency> currencyList 	= new ArrayList<MCurrency>();
		
		sql.append(" select code, name  ");
		sql.append(" from m_currency ");
		sql.append(" where active_status='A' ");
		sql.append(" order by code ");
		
		log.debug("SQL -> " + sql.toString());
		
		pstm = conn.prepareStatement(sql.toString());
		rs = pstm.executeQuery();
		
		while(rs.next()){
			currency = new MCurrency();
			currency.setCode(rs.getString("code"));
			currency.setName(rs.getString("name"));
			currencyList.add(currency);
		}
		
		log.debug("currencyList size = " + currencyList.size());
		
		return currencyList;
	}
	
	public List<CConfigSys> getConfig(Connection conn, PreparedStatement pstm, ResultSet rs) throws Exception {
		StringBuffer sql 				= new StringBuffer();
		CConfigSys config				= null;
		List<CConfigSys> configList 	= new ArrayList<CConfigSys>();

		sql.append(" select *  ");
		sql.append(" from c_config_sys ");
		sql.append(" where active_status='A' ");
		
		log.debug("SQL -> " + sql.toString());
		
		pstm = conn.prepareStatement(sql.toString());
		rs = pstm.executeQuery();
		
		while(rs.next()){
			config = new CConfigSys();
			config.setKeys(rs.getString("keys"));
			config.setValue(rs.getString("value"));
			config.setRowVersion(rs.getInt("row_version"));
			configList.add(config);
		}
		
		log.debug("configList size = " + configList.size());
		
		return configList;
	}
	
	public String updateConfig(Connection conn, PreparedStatement pstm, CConfigSys config) throws Exception {
		StringBuffer sql 		= new StringBuffer();
		
		int update = 0;
		
		sql.append(" update c_config_sys set  ");
		sql.append(" value = ? ,");
		sql.append(" update_by = ? , ");
		sql.append(" update_dt = getDate() , ");
		sql.append(" row_version = ? ");
		sql.append(" where keys = ? ");

		log.debug("SQL -> " + sql.toString());
		
		pstm = conn.prepareStatement(sql.toString());
		pstm.setString(1, config.getValue());
		pstm.setString(2, config.getUpdateBy());
		pstm.setInt(3, config.getRowVersion());
		pstm.setString(4, config.getKeys());
		
		update = pstm.executeUpdate();
		
		log.debug("update = " + update);
		
		return update == 1 ? "s" : "e";
	}
}
